﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml.Packaging;

namespace OPENXMLLib
{
    public static class SheetDataExtention
    {
        /// <summary>
        /// 获取一行
        /// </summary>
        /// <param name="sheetData"></param>
        /// <param name="rowIndex"></param>
        /// <returns></returns>
        public static Row GetRow(this SheetData sheetData, uint rowIndex, bool autoCreate)
        {
            Row row = sheetData.Elements<Row>().Where(p => p.RowIndex == rowIndex).SingleOrDefault();

            if (autoCreate && row == null)
            {
                row = new Row();
                row.RowIndex = (uint)rowIndex;
                sheetData.Append(row);
            }
            return row;
        }

        public static Worksheet GetWorksheet(this SheetData sheetData)
        {
            return (sheetData.Parent as Worksheet);
        }

        public static WorksheetPart GetWorksheetPart(this SheetData sheetData)
        {
            return (sheetData.Parent as Worksheet).WorksheetPart;
        }
    }
}
